﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using Webdiver;

namespace AnyOffice.general.email
{
    public partial class inbox : BasePage
    {
        public void ChangePage(object src, PageChangedEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            this.BindData();
        }
        public string formatSize(long size)
        {
            return StringHelper.FormatSize(size);
        }
        private void BindData()
        {
            int totalIsNew = 0;
            int boxId = Convert.ToInt32(this.ViewState["BoxId"].ToString());
            DataTable table = new EmailDao().InboxSelectPaged(base.Conn, this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex, false, boxId, base.User.Identity.Name, ref totalIsNew);
            this.ViewState["CurrentRowNums"] = table.Rows.Count;
            this.rptEmail.DataSource = table;
            this.rptEmail.DataBind();
            this.litNavCount.Text = "<span class=\"small1\">共&nbsp;<span class=\"big4\">" + this.AspNetPager1.RecordCount.ToString() + "</span>&nbsp;封邮件，<span class=\"big4\">" + totalIsNew.ToString() + "&nbsp;</span>&nbsp;封为新邮件</span>";
            this.litNavPager.Text = "<span class=\"small1\">当前为第" + this.AspNetPager1.CurrentPageIndex.ToString() + "页，共" + this.AspNetPager1.PageCount.ToString() + "页，每页最多" + this.AspNetPager1.PageSize.ToString() + "封</span>";
            this.ViewState["CurrentPage"] = this.AspNetPager1.CurrentPageIndex.ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                int boxId = (Request.QueryString["box_id"] != null) ? int.Parse(Request.QueryString["box_id"].ToString()) : 0;
                this.ViewState["BoxId"] = boxId;
                EmailBoxDao dao = new EmailBoxDao();
                string str = string.Empty;
                if (boxId == 0)
                {
                    str = "收件箱";
                }
                else
                {
                    DataTable table = dao.SelectByBoxId(base.Conn, boxId, base.User.Identity.Name);
                    if (table.Rows.Count > 0)
                    {
                        str = table.Rows[0]["BoxName"].ToString();
                    }
                }
                this.litNavTitle.Text = str;
                this.ViewState["BoxName"] = str;
                this.AspNetPager1.PageSize = new ConfigManager().PageSize;
                int totalIsNew = 0;
                DataTable table2 = new EmailDao().InboxSelectPaged(base.Conn, this.AspNetPager1.PageSize, 1, true, boxId, base.User.Identity.Name, ref totalIsNew);
                if (int.Parse(table2.Rows[0][0].ToString()) == 0)
                {
                    this.pnlMain.Visible = false;
                    this.litNavCount.Visible = false;
                    this.litNavPager.Visible = false;
                    this.litMessage.Text = base.ShowMessage(str + "无邮件");
                    return;
                }
                this.AspNetPager1.RecordCount = int.Parse(table2.Rows[0][0].ToString());
                this.BindData();
                DataTable table3 = dao.SelectAll(base.Conn, base.User.Identity.Name);
                this.sltBoxId.DataSource = table3;
                this.sltBoxId.DataTextField = "BoxName";
                this.sltBoxId.DataValueField = "BoxId";
                this.sltBoxId.DataBind();
                this.sltBoxId.Items.Insert(0, new ListItem("收件箱", "0"));
                if (this.sltBoxId.Items.FindByValue(boxId.ToString()) != null)
                {
                    this.sltBoxId.Items.FindByValue(boxId.ToString()).Selected = true;
                }
                this.btnDelete.Attributes.Add("onclick", "return delete_mail();");
                this.sltBoxId.Attributes.Add("onchange", "change_box();");
            }
            if (this.Page.IsPostBack)
            {
                int num3 = 0;
                int num4 = 0;
                int num5 = Convert.ToInt32(this.ViewState["BoxId"].ToString());
                EmailDao dao3 = new EmailDao();
                foreach (RepeaterItem item in this.rptEmail.Items)
                {
                    HtmlInputCheckBox box = (HtmlInputCheckBox)item.FindControl("EmailId");
                    if ((box != null) && box.Checked)
                    {
                        dao3.Delete(base.Conn, int.Parse(box.Value), EmailDao.DeleteType.Inbox, base.User.Identity.Name);
                        num4++;
                    }
                }
                if (num4 == Convert.ToInt32(this.ViewState["CurrentRowNums"].ToString()))
                {
                    DataTable table4 = dao3.InboxSelectPaged(base.Conn, this.AspNetPager1.PageSize, 1, true, num5, base.User.Identity.Name, ref num3);
                    this.AspNetPager1.RecordCount = int.Parse(table4.Rows[0][0].ToString());
                    if (int.Parse(table4.Rows[0][0].ToString()) == 0)
                    {
                        this.pnlMain.Visible = false;
                        this.litNavCount.Visible = false;
                        this.litNavPager.Visible = false;
                        this.litMessage.Text = base.ShowMessage(this.ViewState["BoxName"].ToString() + "无邮件");
                    }
                    else
                    {
                        if (this.AspNetPager1.CurrentPageIndex > 1)
                        {
                            this.AspNetPager1.CurrentPageIndex--;
                        }
                        this.BindData();
                    }
                }
            }
        }

 
    }
}
